package cn.unknowpupil.init.service.system.impl;

import cn.unknowpupil.init.configure.mybatisplus.base.BasePage;
import cn.unknowpupil.init.utils.strings.StringUtils;
import cn.unknowpupil.init.domain.system.SysDictData;
import cn.unknowpupil.init.mapper.system.SysDictDataMapper;
import cn.unknowpupil.init.service.system.ISysDictDataService;
import cn.unknowpupil.init.service.system.ISysDictTypeService;
import cn.unknowpupil.init.utils.strings.DictUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created at 2020-10-30 by  w10g <br>
 * Initialize SysDictData  ... <br>
 * 字典数据表 核心业务处理实现类
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService {


    /**
     * 新增
     *
     * @param sysDictData {@link SysDictData}  字典数据表
     * @return {@link SysDictData}
     */
    @Override
    public boolean create(SysDictData sysDictData) {
        return save(sysDictData);
    }

    /**
     * 详情
     *
     * @param id {@code Long}   SysDictData ID
     * @return {@link SysDictData}
     */
    @Override
    public SysDictData info(Long id) {
        return getById(id);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}   SysDictData ID
     * @return {@link SysDictData}
     */
    @Override
    public boolean delete(Long[] ids) {

        List<Long> collect = Stream.of(ids).collect(Collectors.toList());
        return removeByIds(collect);
    }


    /**
     * 修改
     *
     * @param sysDictData {@link SysDictData}  字典数据表
     * @return {@link SysDictData}
     */
    @Override
    public boolean update(SysDictData sysDictData) {
        return updateById(sysDictData);
    }


    /**
     * 分页
     *
     * @param current     {@code int} 页码
     * @param size        {@code int} 笔数
     * @param sysDictData {@link SysDictData}
     * @return {@code IPage<SysDictData>}
     */
    @Override
    public IPage<SysDictData> page(int current, int size, SysDictData sysDictData) {
        IPage<SysDictData> page = new Page<>(current, size);
//        Page<SysDictData> instance = BasePage.getInstance(sysDictData);
        LambdaQueryWrapper<SysDictData> wrapper = wrapper(sysDictData);
        wrapper.orderByDesc(SysDictData::getDictSort);
        return super.page(page, wrapper);
    }

    @Override
    public List<SysDictData> selectDictDataByType(String dictType) {
        List<SysDictData> dictDatas = DictUtils.getDictCache(dictType);
        if (!StringUtils.isNotEmpties(dictDatas)) {
            LambdaQueryWrapper<SysDictData> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SysDictData::getDictType,dictType);
            wrapper.eq(SysDictData::getStatus, 0);
            wrapper.orderByAsc(SysDictData::getDictSort);
            dictDatas = super.list(wrapper);
            if (StringUtils.isNotEmpties(dictDatas)) {
                DictUtils.setDictCache(dictType, dictDatas);
            }
        }

        return dictDatas;
    }

    @Override
    public List<SysDictData> selectDictDataList(SysDictData sysDictData)
    {
        LambdaQueryWrapper<SysDictData> wrapper = wrapper(sysDictData);
        return super.list(wrapper);
    }

    private LambdaQueryWrapper<SysDictData> wrapper(SysDictData sysDictData){
        LambdaQueryWrapper<SysDictData> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(sysDictData.getDictType())) {
            wrapper.eq(SysDictData::getDictType, sysDictData.getDictType());
        }
        if (StringUtils.isNotBlank(sysDictData.getDictLabel())) {
            wrapper.like(SysDictData::getDictLabel, sysDictData.getDictLabel());
        }
        if (StringUtils.isNotBlank(sysDictData.getStatus())) {
            wrapper.eq(SysDictData::getStatus, sysDictData.getStatus());
        }
        return wrapper;
    }

}
